<template>
  <header class="header">
    <div class="container">
      <div class="header-content">
        <!-- Logo -->
        <div class="logo" @click="goHome">
          <h1>{{ $t('nav.home') === 'Home' ? 'Web Mall' : 'Web商城' }}</h1>
        </div>
        
        <!-- 搜索框 -->
        <div class="search-box">
          <a-input-search
            v-model:value="searchKeyword"
            :placeholder="$t('nav.search')"
            size="large"
            @search="handleSearch"
            @press-enter="handleSearch"
          >
            <template #enterButton>
              <a-button type="primary" size="large">
                <template #icon>
                  <SearchOutlined />
                </template>
                {{ $t('common.search') }}
              </a-button>
            </template>
          </a-input-search>
        </div>
        
        <!-- 右侧菜单 -->
        <div class="header-menu">
          <!-- 语言切换器 -->
          <LanguageSwitcher />
          
          <!-- 购物车 -->
          <div class="cart-icon" @click="toggleCart">
            <a-badge :count="cartItemCount" :offset="[15, 0]">
              <ShoppingCartOutlined class="menu-icon" />
            </a-badge>
            <span class="menu-text">{{ $t('nav.cart') }}</span>
          </div>
          
          <!-- 用户菜单 -->
          <div v-if="isLoggedIn" class="user-menu">
            <a-dropdown>
              <div class="user-info">
                <a-avatar :src="userInfo?.avatar" size="small">
                  {{ userInfo?.username?.charAt(0)?.toUpperCase() }}
                </a-avatar>
                <span class="username">{{ userInfo?.username }}</span>
                <DownOutlined />
              </div>
              <template #overlay>
                <a-menu>
                  <a-menu-item key="profile" @click="goToProfile">
                    <UserOutlined />
                    {{ $t('nav.profile') }}
                  </a-menu-item>
                  <a-menu-item key="orders" @click="goToOrders">
                    <FileTextOutlined />
                    {{ $t('nav.orders') }}
                  </a-menu-item>
                  <a-menu-item key="favorites" @click="goToFavorites">
                    <HeartOutlined />
                    {{ $t('nav.favorites') }}
                  </a-menu-item>
                  <a-menu-divider />
                  <a-menu-item key="logout" @click="handleLogout">
                    <LogoutOutlined />
                    {{ $t('nav.logout') }}
                  </a-menu-item>
                </a-menu>
              </template>
            </a-dropdown>
          </div>
          
          <!-- 登录/注册按钮 -->
          <div v-else class="auth-buttons">
            <a-button type="link" @click="goToLogin">{{ $t('nav.login') }}</a-button>
            <a-button type="primary" @click="goToRegister">{{ $t('nav.register') }}</a-button>
          </div>
        </div>
      </div>
      
      <!-- 导航菜单 -->
      <nav class="nav-menu">
        <ul class="nav-list">
          <li class="nav-item" @click="goHome">
            <HomeOutlined />
            {{ $t('nav.home') }}
          </li>
          <li class="nav-item" @click="goToProducts">
            <AppstoreOutlined />
            {{ $t('nav.products') }}
          </li>
          <li class="nav-item" @click="goToCategory('手机数码')">
            <MobileOutlined />
            手机数码
          </li>
          <li class="nav-item" @click="goToCategory('电脑办公')">
            <LaptopOutlined />
            电脑办公
          </li>
          <li class="nav-item" @click="goToCategory('家用电器')">
            <HomeOutlined />
            家用电器
          </li>
          <li class="nav-item" @click="goToAbout">
            <InfoCircleOutlined />
            关于我们
          </li>
        </ul>
      </nav>
    </div>
    
    <!-- 购物车侧边栏 -->
    <a-drawer
      title="购物车"
      placement="right"
      :open="isCartOpen"
      @close="closeCart"
      width="400"
    >
      <CartSidebar />
    </a-drawer>
  </header>
</template>

<script setup>
import { ref, computed } from 'vue'
import { useStore } from 'vuex'
import { useRouter } from 'vue-router'
import { message, Modal } from 'ant-design-vue'
import {
  SearchOutlined,
  ShoppingCartOutlined,
  UserOutlined,
  FileTextOutlined,
  HeartOutlined,
  LogoutOutlined,
  HomeOutlined,
  AppstoreOutlined,
  MobileOutlined,
  LaptopOutlined,
  InfoCircleOutlined,
  DownOutlined
} from '@ant-design/icons-vue'
import CartSidebar from './CartSidebar.vue'
import LanguageSwitcher from './LanguageSwitcher.vue'

// 使用Vuex store和Vue Router
const store = useStore()
const router = useRouter()

// 响应式数据
const searchKeyword = ref('')

// 计算属性
const isLoggedIn = computed(() => store.getters['user/isLoggedIn'])
const userInfo = computed(() => store.getters['user/userInfo'])
const cartItemCount = computed(() => store.getters['cart/cartItemCount'])
const isCartOpen = computed(() => store.getters['cart/isCartOpen'])

// 方法
const goHome = () => {
  router.push('/')
}

const handleSearch = () => {
  if (searchKeyword.value.trim()) {
    router.push({
      name: 'Search',
      query: { q: searchKeyword.value.trim() }
    })
  }
}

const toggleCart = () => {
  store.dispatch('cart/toggleCart')
}

const closeCart = () => {
  store.dispatch('cart/setCartOpen', false)
}

const goToLogin = () => {
  router.push('/login')
}

const goToRegister = () => {
  router.push('/register')
}

const goToProfile = () => {
  router.push('/profile')
}

const goToOrders = () => {
  router.push('/orders')
}

const goToFavorites = () => {
  router.push('/favorites')
}

const goToProducts = () => {
  router.push('/products')
}

const goToCategory = (category) => {
  router.push(`/category/${category}`)
}

const goToAbout = () => {
  router.push('/about')
}

const handleLogout = () => {
  Modal.confirm({
    title: $t('common.confirm'),
    content: $t('user.logoutConfirm') || '您确定要退出登录吗？',
    onOk: async () => {
      try {
        await store.dispatch('user/logout')
        message.success($t('user.logoutSuccess'))
        router.push('/')
      } catch (error) {
        message.error($t('user.logoutFailed') || '退出登录失败')
      }
    }
  })
}
</script>

<style scoped>
.header {
  background-color: white;
  box-shadow: 0 2px 8px rgba(0, 0, 0, 0.1);
  position: sticky;
  top: 0;
  z-index: 1000;
}

.header-content {
  display: flex;
  align-items: center;
  justify-content: space-between;
  padding: 15px 0;
}

.logo {
  cursor: pointer;
  flex-shrink: 0;
}

.logo h1 {
  margin: 0;
  font-size: 24px;
  font-weight: bold;
  color: #1890ff;
}

.search-box {
  flex: 1;
  max-width: 500px;
  margin: 0 20px;
}

.header-menu {
  display: flex;
  align-items: center;
  gap: 20px;
  flex-shrink: 0;
}

.cart-icon {
  display: flex;
  flex-direction: column;
  align-items: center;
  cursor: pointer;
  padding: 8px;
  border-radius: 4px;
  transition: background-color 0.3s;
}

.cart-icon:hover {
  background-color: #f5f5f5;
}

.menu-icon {
  font-size: 20px;
  color: #666;
}

.menu-text {
  font-size: 12px;
  color: #666;
  margin-top: 2px;
}

.user-menu {
  cursor: pointer;
}

.user-info {
  display: flex;
  align-items: center;
  gap: 8px;
  padding: 8px;
  border-radius: 4px;
  transition: background-color 0.3s;
}

.user-info:hover {
  background-color: #f5f5f5;
}

.username {
  font-size: 14px;
  color: #333;
}

.auth-buttons {
  display: flex;
  gap: 10px;
}

/* 导航菜单 */
.nav-menu {
  border-top: 1px solid #f0f0f0;
  padding: 10px 0;
}

.nav-list {
  display: flex;
  list-style: none;
  margin: 0;
  padding: 0;
  gap: 30px;
}

.nav-item {
  display: flex;
  align-items: center;
  gap: 5px;
  cursor: pointer;
  padding: 8px 12px;
  border-radius: 4px;
  transition: all 0.3s;
  color: #666;
}

.nav-item:hover {
  background-color: #f5f5f5;
  color: #1890ff;
}

/* 响应式设计 */
@media (max-width: 768px) {
  .header-content {
    flex-direction: column;
    gap: 15px;
  }
  
  .search-box {
    width: 100%;
    max-width: none;
    margin: 0;
  }
  
  .header-menu {
    width: 100%;
    justify-content: space-between;
  }
  
  .nav-list {
    flex-wrap: wrap;
    gap: 15px;
  }
  
  .nav-item {
    font-size: 14px;
  }
  
  .auth-buttons {
    flex-direction: column;
    gap: 5px;
  }
}
</style> 